package com.wangwenjun.concurrency.chapter17;


import java.util.concurrent.Semaphore;

/**
 *
 * 3个线程轮流打印123各10次,使用semaphore
 *
 */
public class TaoBaoTest12 {


    public static void main(String[] args) throws InterruptedException {
        Semaphore semaphore1 = new Semaphore(1);
        Semaphore semaphore2 = new Semaphore(0);
        Semaphore semaphore3 = new Semaphore(0);


        new Thread(()->{
            for (int i = 1; i <= 10 ; i++) {
                try {
                    semaphore3.acquire();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("3");
                semaphore1.release();
            }
        }, "t3").start();

        new Thread(()->{
            for (int i = 1; i <= 10 ; i++) {
                try {
                    semaphore2.acquire();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("2");
                semaphore3.release();
            }
        }, "t2").start();

        new Thread(()->{
            for (int i = 1; i <= 10 ; i++) {
                try {
                    semaphore1.acquire();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("1");
                semaphore2.release();
            }
        }, "t1").start();




    }









}
